home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_069 / spool / prtspool.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  6KB  |  275 lines

  1. /* PRTSPOOL.c - print SPOOLed file program */
  2.  
  3. /*
  4. **    Date written: 04/23/86
  5. **    Author: Tim Holloway
  6. **        Compuserve: 73026,2026
  7. **        Bix: tholloway
  8. **        Fido node: 112/1 (Casa Mi Amiga).
  9. **
  10. **    Version: 1.1 - a true spooler version
  11. **
  12. **    Copyright (C) 1986, by Tim Holloway.  This program may be
  13. **    freely distributed for non-commercial use only.  Use for commercial
  14. **    purposes without the express permission of the author is a violation
  15. **    of copyright law.
  16. **
  17. **    Description:
  18. **       This program accepts a printer name (file name) as a parameter.
  19. **    It opens the file and requests the names of files to be printed
  20. **    by sending messages to the SPOOLER program, which fills in a
  21. **    file name and returns the message to PRTSPOOL
  22. **
  23. **    Change History: 
  24. **
  25. **    11/19/86, TFH. added logic to actually drive a printer.
  26. **        removed the tracing code, and changed the error message
  27. **        reporting to use my Gripe procedure so display a requester
  28. **        if the SPOOLED file could not be accessed.
  29. **
  30. **    Usage: See below.
  31. **
  32. **    Special notes: sorry, I mixed AmigaDOS file I/O and Unix-style
  33. **    I/O.  In accordance with my policy of never programming unportable
  34. **    unless neccessary, I had intended to use all Unix-style I/O, but
  35. **    the Lattice version 3.03 and earlier systems buffer files that
  36. **    according to Unix should not be buffered.  I could have used
  37. **    the set-non-buffered function, but two wrongs don't make a right!
  38. */
  39.  
  40. /* Basic Amiga system definitions */
  41.  
  42. #include <exec/types.h>
  43. #include <exec/alerts.h>
  44. #include <libraries/dos.h>
  45. #include <libraries/dosextens.h>
  46. #include <graphics/gfx.h>
  47. #include <intuition/intuition.h>
  48.  
  49. struct Library *IntuitionBase, *OpenLibrary();
  50.  
  51. /* more mundane definitions */
  52.  
  53. #include "stdio.h"
  54. #include "spool.h"
  55.  
  56. /* define login and logout stuff in the following #include: */
  57.  
  58. #include "login.cx"
  59.  
  60. struct Process *myprocess, *FindTask();
  61. struct FileHandle *outfile, *Open();
  62.  
  63. BOOL io_error = FALSE,
  64.      aborting = FALSE;
  65.  
  66. usage()
  67. {
  68.     printf ("PRTSPOOL program v1.1 by Tim Holloway\n");
  69.     printf ("Usage: PRTSPOOL filename\n");
  70. }
  71.  
  72. SPOOLmsg packet = {{NULL, NULL, 0}, '?', ""};
  73.  
  74. error (msg)
  75. char *msg;
  76. {
  77.     Gripe ("PRTSPOOL:", msg, "");
  78. }
  79.  
  80. /***************************************************************************/
  81.  
  82. static int linenum = 0;
  83. static char linebuffer[256];
  84. FILE * infile;
  85.  
  86. #define LINES_PER_PAGE 56
  87.  
  88. static int hour, minute, year, day, month;
  89.  
  90. void
  91. getdate()
  92. {
  93.    long dates[3]; int jday;
  94.  
  95.    (void) DateStamp(dates);
  96.    hour = dates[1] / 60;
  97.    minute = dates[1] % 60;
  98.    year = (dates[0] * 4) / 1461;
  99.    jday = dates[0] - (year*1461)/4;
  100.    year += 1978;
  101.    Julian_to_Gregorian (jday, &year, &month, &day);
  102. }
  103.  
  104. void xputs(str)
  105. {
  106.     register int i;
  107.  
  108.     i = strlen(str);
  109.     if (Write (outfile, str, i) != i)
  110.         io_error = TRUE;
  111. }
  112.  
  113. spool_file(fname)        /* print a file */
  114. char *fname;
  115. {
  116.    register int rc;
  117.  
  118.    /* Don't use Gripe, here! - it would halt PRTSPOOL until the user */
  119.    /* acknowledged.  Better to waste a page and keep the printer running. */
  120.  
  121.    if ((infile = fopen (fname, "r")) == 0)
  122.    {
  123.       xputs ("\f\n\n\n");
  124.       xputs ("*******************************************\n");
  125.       xputs ("**** ERROR ********************************\n");
  126.       xputs ("Could not open file to be printed:\n");
  127.       xputs (fname); xputs("\n");
  128.       xputs ("*******************************************\n");
  129.       return;
  130.    }
  131.  
  132.    getdate();        /* get timestamp */
  133.    getheader();        /* setup page header */
  134.    putheader();
  135.  
  136.    do
  137.    {
  138.       put_a_line();
  139.    } while (NOT io_error && get_a_line());
  140.    if (io_error)
  141.    {
  142.     io_error = FALSE;
  143.     aborting = TRUE;
  144.     Gripe ("PRTSPOOL Terminating:", "I/O error on printer file.", "");
  145.    }
  146.    xputs ("\n");
  147.    xputs ("EOF\n");
  148.    rc = fclose (infile);
  149.  
  150. /******** do NOT do the folowing under Lattice C version 3.03 and earlier.
  151.   They didn't realize the the Amiga Close function does not return an
  152.   error code.
  153.  
  154.    if (rc != 0) Gripe ("PRTSPOOL:", "Error closing file being printed", "");
  155. *********/
  156. }
  157.  
  158. int
  159. get_a_line()
  160. {
  161.    char *i;
  162.  
  163.    i = fgets(linebuffer, sizeof(linebuffer), infile);
  164.    return (i != NULL);
  165. }
  166.  
  167. put_a_line()
  168. {
  169.    if (linenum++ > LINES_PER_PAGE) putheader();
  170.  
  171.    xputs (linebuffer);
  172. }
  173.  
  174. #define HBSIZE 32+8+2+6
  175. static char headbuffer[HBSIZE+8];
  176.  
  177. /*
  178.   two for ff, stretch, 32 for title, 8 for page#, 4 for shrink, 2*NL, NUL
  179. */
  180.  
  181. getheader()
  182. {
  183.    int i;
  184.  
  185.    fgets (linebuffer, 255, infile);
  186.    headbuffer[0] = 0x0c;   /* ff */
  187.    headbuffer[1] = 0x0e;   /* stretch */
  188.    for (i = 0; (i<HBSIZE-14) && (linebuffer[i] >= ' '); i++)
  189.       headbuffer[i+2] = linebuffer[i];
  190.    i+=2;
  191.    headbuffer[i++]= 0x0f;
  192.    headbuffer[i++]= '\n';
  193.    headbuffer[i++]= '\0';
  194. }
  195.  
  196. putheader()
  197. {
  198.    char outfb[81];
  199.  
  200.    xputs(headbuffer);
  201.    sprintf (outfb, "As of: %02.2d/%02.2d/%4d, %02d:%02.2d\n\n",
  202.                              month, day, year, hour, minute);
  203.    xputs(outfb);
  204.    linenum = 0;
  205. }
  206.  
  207. /***************************************************************************/
  208.  
  209. main (argc, argv)
  210. int argc;
  211. char *argv[];
  212. {
  213.    PORTPAIR ports;
  214.  
  215.    if ( (IntuitionBase = OpenLibrary("intuition.library", 0)) == NULL)
  216.    {
  217.     Alert (AT_Recovery + AG_OpenLib + AO_Intuition, NULL);
  218.     exit(20);
  219.    }
  220.  
  221.    if ((argc < 2) || (argv[1][1] == '?'))
  222.    {
  223.     usage();
  224.     goto abort;
  225.    }
  226.  
  227.    if ( (outfile= Open(argv[1], MODE_OLDFILE)) == NULL)
  228.    {
  229.     Gripe ("PRTSPOOL:", "Couldn't open printer file:", argv[1]);
  230.     goto abort;
  231.    }
  232.  
  233.    if (NOT LogInPort (GIMME_A_FILE, &ports, &packet))
  234.    {
  235.     error ("SPOOLER program is inactive");
  236.     goto abort;
  237.    }
  238.  
  239. /*
  240. printf ("message port for %s is at %lx\n",
  241.  GIMME_A_FILE, ports[OUTPORT]);
  242. */
  243.  
  244.     /* raise priority - this program is low-overhead (mostly Waiting)    */
  245.  
  246.    myprocess = FindTask(NULL);
  247.    (void) SetTaskPri (myprocess, PRTSPOOL_PRIORITY);
  248.  
  249.    /* The following loop goes out and solicits for a filename.        */
  250.  
  251.    while (NOT aborting)
  252.    {
  253.       packet.filename[0] = NUL;    /* be neat, null out file name */
  254.       packet.minfo.mn_ReplyPort = ports[INPORT];    /* return address */
  255.  
  256. /* printf ("send/w reply port at %lx\n", packet.minfo.mn_ReplyPort); */
  257.  
  258.       packet.minfo.mn_Length = sizeof(packet.filename);
  259.  
  260. /* printf ("===request to spool...\n"); */
  261.  
  262.       PutMsg (ports[OUTPORT], &packet);
  263.       WaitPort(ports[INPORT]);
  264.       (VOID) GetMsg (ports[INPORT]);
  265.       if (packet.log_status == LOG_OUT) break;
  266.       spool_file(packet.filename);
  267.    }
  268.  
  269.    Close(outfile);
  270.    LogOutPort (&ports, &packet);
  271.  
  272. abort:
  273.    CloseLibrary(IntuitionBase);
  274. }
  275.